#include "User.h"
#include<tntdb/result.h>
#include<tntdb/error.h>

User::User() {
    isvalid = false;
};

bool User::validate(string name, string pass, tntdb::Connection* conn) {
    tntdb::Result result;
    result = conn->select("select password from dono_user where login like '" + name + "'");
    if(result.empty()) {
		isvalid = false;
        return false;
    }
    if(pass != result.getValue(0,0).getString()) {
		isvalid = false;
        return false;
    }
    user_name = name;
    isvalid = true;
    return true;
};

string User::create(string name, string pass, tntdb::Connection* conn) {
	try {
		conn->execute("insert into dono_user (id,login,password,is_admin,ip) \
		values (nextval('dono_userid_seq'),'"+name+"','"+pass+"',false,'127.0.0.1')");
		user_name = name;
		isvalid = true;
		return "Success";
	} catch (const tntdb::Error& e) {
		return e.what();
	}
}

void User::logout() {
	isvalid = false;
}

string User::getName() {
    return user_name;
};
